package com.fbt.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.energy.dao.SpEquipmentDao;
import com.fbt.energy.entity.SpEquipment;
import com.fbt.energy.service.SpEquipmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 备件_设备表(SpEquipment)表服务实现类
 *
 * @author makejava
 * @since 2022-01-21 14:30:41
 */
@Service("spEquipmentService")
public class SpEquipmentServiceImpl extends ServiceImpl<SpEquipmentDao, SpEquipment> implements SpEquipmentService {
    @Resource
    private SpEquipmentDao spEquipmentDao;

    @Resource
    private SpEquipmentService spEquipmentService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SpEquipment queryById(Integer id) {
        return this.spEquipmentDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param spEquipment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SpEquipment> queryByPage(SpEquipment spEquipment, PageRequest pageRequest) {
        long total = this.spEquipmentDao.count(spEquipment);
        return new PageImpl<>(this.spEquipmentDao.queryAllByLimit(spEquipment, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param spEquipment 实例对象
     * @return 实例对象
     */
    @Override
    public SpEquipment insert(SpEquipment spEquipment) {
        this.spEquipmentDao.insert(spEquipment);
        return spEquipment;
    }

    /**
     * 修改数据
     *
     * @param spEquipment 实例对象
     * @return 实例对象
     */
    @Override
    public SpEquipment update(SpEquipment spEquipment) {
        this.spEquipmentDao.update(spEquipment);
        return this.getEqByEqNo(spEquipment.getEqNo());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String eqNo) {
        return this.spEquipmentDao.deleteById(eqNo) > 0;
    }

    @Override
    public List<SpEquipment> getSpEquipments(String prNo) {

        QueryWrapper<SpEquipment> spEquipmentQueryWrapper=new QueryWrapper<>();
        spEquipmentQueryWrapper.eq("pr_no", prNo);
        List<SpEquipment> eqList = spEquipmentService.list(spEquipmentQueryWrapper);
        return eqList;
    }

    /**查询产线下所有设备*/
    @Override
    public List<SpEquipment> getAllEqByPrNo(String prNo) {
        QueryWrapper<SpEquipment>spEquipmentQueryWrapper=new QueryWrapper<>();
        QueryWrapper<SpEquipment> eqs = spEquipmentQueryWrapper.eq("pr_no", prNo);
        List<SpEquipment> spEquipments = spEquipmentService.list(eqs);
        return spEquipments;
    }

    @Override
    public SpEquipment getEqByEqNo(String eqNo) {

        QueryWrapper<SpEquipment> equipmentQueryWrapper= new QueryWrapper<>();
        equipmentQueryWrapper.eq("eq_no", eqNo);
        SpEquipment serviceOne = spEquipmentService.getOne(equipmentQueryWrapper);
        return serviceOne;
    }
}
